<%@ page contentType="text/html" pageEncoding="UTF-8" import="com.almori.hr.timetracker.*" import="java.io.File" import="java.util.*" import="java.sql.*" import="java.text.*" import="java.text.*" %>


<%
    try {
        MgrDashBean mb = new MgrDashBean();
        TimeTrackerBean bean = new TimeTrackerBean();
        Employee me = (Employee) session.getAttribute("Employee");

        if (me.getRole() == 2) {
            File f = new File(getServletContext().getRealPath("") + "\\img\\pics\\" + me.getPic());
            String p = (f.exists()) ? me.getPic() : "auser.jpg";  // check for pic or get generic one*/
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Timetracker Almori BPO Services - Edit Timecards</title>
        <link rel="Shortcut icon" href="img/logo.png" />
        <link rel="stylesheet" type="text/css" href="css/style_mgr.css" />
        <!--[if IE 8]>
         <link rel="stylesheet" type="text/css" href="css/style_mrIE.css" />
      <![endif]-->
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script language="javascript" type="text/javascript">
<!--
            function popitup(url) {
                newwindow = window.open(url, 'name', 'toolbar=yes, location=yes, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=yes, width=450, height=395');
                if (window.focus) {
                    newwindow.focus()
                }
                return false;
            }


// -->
        </script>
    </head>
    <body>
        <div class="container">

            <div id="signup">

                <div class="top_left">
                    <img src="img/company.logo.19.jpg" alt="company logo"></img>
                </div>

                <div class="small_user_pic">
                    <a href="punchit.jsp"><img src="img/pics/<%= p%>" alt="pic" height="33" width="33"></img></a>
                    <a href="index.jsp"><img src="img/log-out.png" alt="sign out" height="33" width="33" ></img></a>
                </div>

                <div class="header">
                    <img src="img/logo.png" alt="logo"></img>
                </div> 

                <div align="center">
                    <h2 color="blue">EDITAR FICHA DE TIEMPO</h2>
                </div>

                <div class="inputs">

                    <div id="boxed-center">
                        <form action="#" method="GET">
                            <div class="inputs">
                                <%
                                    String E = request.getParameter("employee");
                                    String Q = request.getParameter("15days");
                                    String D = request.getParameter("dept");
                                    String deparmentName = request.getParameter("_departmentName");
                                    if (D == null || D.equals("")) { //First time in load %> 
                                <label float="left">Departamento: </label>    
                                <select name="dept" id="combo" onChange="location = this.options[this.selectedIndex].value;"><br />
                                    <option></option>
                                    <%
                                        List<Department> departments = new ArrayList();
                                        departments = bean.getDepartmentsList();
                                        out.println("<p>Departments </p>");
                                        try {
                                            for (int i = 0; i < departments.size(); i++) {
                                                out.println("<option value='?dept=" + departments.get(i).getId() + "'>" + departments.get(i).getDescription() + "</option>");
                                            }

                                        } catch (Exception e) {
                                            out.println("<option value='0'>Error: cannot access DataBase!</option>");
                                        }
                                    %> 

                                </select>
                                <br /><br />
                                <%        }%>
                                <div id="employees">
                                    <%
                                        try {
                                            if (D != null) {
                                                int depid = Integer.parseInt(D);
                                                if (E != null) { //populate combo by empl id
                                                    int emid = Integer.parseInt(E);
                                                    int q = Integer.parseInt(Q);
                                                    out.println(mb.getEmployeeListHTML(depid, deparmentName, emid, q));
                                                } else {
                                                    out.println(mb.getEmployeeListHTML(depid));
                                                }

                                            } else {
                                            }

                                        } catch (Exception e) {
                                        }
                                    %>

                                </div>


                            </div>
                        </form>
                        <div id="results">

                            <%
                                try {
                                    //       String E = request.getParameter("employee");
                                    //       String Q = request.getParameter("15days");
                                    if (D != null && E != null && Q != null) {
                                        //////////////// Employees
                                        //   int S = Integer.parseInt(D);
                                        //   out.println(mb.getEmployeeListHTML(S));
                                        int id_empl = Integer.parseInt(E);
                                        Employee searchedEmpl = bean.getEmployee(id_empl);
                            %>   
                            <style type="text/css">
                                #editTable {
                                    border: 2px solid #CCCCCC;
                                    border-collapse: collapse;
                                    width: 700px; 
                                    font-family: Cambria, Cochin, Georgia, serif;
                                    font-size: 14px;
                                    margin-left: auto;
                                    margin-right: auto;
                                }
                                #editTable td, #editTable th {
                                    border: 1px solid #CCC;
                                    padding: 10px;
                                }

                                #editTable th:nth-of-type(2) {
                                    text-align: center;
                                }

                                #editTable td:nth-of-type(1), #editTable th:nth-of-type(1) {
                                    width: 85px; 
                                    text-align: center;
                                }

                                #editTable th {
                                    font-weight: bold;
                                    font-size: 16px;
                                    /*          background-color: #DDE9FF;*/
                                    background:#74A6BD;
                                    color:#FFFFFF
                                }

                                #editTable tr:nth-of-type(2n + 2) {
                                    background-color: #ECF7FF;
                                }

                                #editTable tr:nth-of-type(2n + 3) {
                                    background-color: #FBFDFF;
                                }
                            </style> 

                            <table id="editTable">
                                <tr>
                                    <th>Fecha</th>
                                    <th>Entrada</th>
                                    <th>Salida</th>
                                    <th>Razón</th>
                                </tr>

                                <%
                                            SimpleDateFormat ft = new SimpleDateFormat("hh:mm a");
                                            Calendar cal = Calendar.getInstance();
                                            int day = cal.get(Calendar.DATE);
                                            int month = cal.get(Calendar.MONTH) + 1;
                                            int year = cal.get(Calendar.YEAR);
                                            int q = Integer.parseInt(Q);
                                            // I need the current 15 days of the month and last ones of last month
                                            if (day < 16) {

                                                if (q == 2) {
                                                 //   out.println("Print current 15th days");
                                                    //   out.println("<br />");

                                                    for (int i1 = 0; i1 < day; i1++) {
                                                        List<Timecard> timecards = bean.getDatesByDate(year + "-" + month + "-" + (i1 + 1), id_empl);

                                                        if (timecards != null && timecards.size() > 0) {

                                                            for (int i = 0; i < timecards.size(); i++) {
                                                                out.println("<tr>");

                                                                out.println("<td style=\"text-align: center;\"><a onclick=\"return popitup('timecard.jsp?id=" + timecards.get(i).getId() + "')\" href=\"timecard.jsp?id=" + timecards.get(i).getId() + "\">" + year + "-" + month + "-" + (i1 + 1) + "</a></td>");

                                                                // out.print(timecards.get(i).getId() + " - ");
                                                                if (timecards.get(i).getIn() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(i).getIn()) + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }
                                                                if (timecards.get(i).getOut() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(i).getOut()) + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }
                                                                if (timecards.get(i).getReason() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + timecards.get(i).getReason() + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }

                                                                out.println("</tr>");
                                                            }
                                                        }
                                                    }
                                                } else {
                                                  //  out.println("Print last 15th days");
                                                    //  out.println("<br />");
                                                    if (month == 1) {
                                                        year--;
                                                        month = 12;
                                                    } else {
                                                        month--;
                                                    }
                                                    for (int i = 15; i < cal.getActualMaximum(cal.DAY_OF_MONTH); i++) {
                                                     //   out.println(year + "-" + month + "-" + (i + 1));
                                                        //  out.println("<br />");
                                                        List<Timecard> timecards = bean.getDatesByDate(year + "-" + month + "-" + (i + 1), id_empl);

                                                        if (timecards != null && timecards.size() > 0) {

                                                            for (int k = 0; k < timecards.size(); k++) {
                                                                out.println("<tr>");

                                                                out.println("<td style=\"text-align: center;\"><a onclick=\"return popitup('timecard.jsp?id=" + timecards.get(k).getId() + "')\" href=\"timecard.jsp?id=" + timecards.get(k).getId() + "\">" + year + "-" + month + "-" + (i + 1) + "</a></td>");

                                                                // out.print(timecards.get(i).getId() + " - ");
                                                                if (timecards.get(k).getIn() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(k).getIn()) + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }
                                                                if (timecards.get(k).getOut() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(k).getOut()) + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }
                                                                if (timecards.get(k).getReason() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + timecards.get(k).getReason() + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }

                                                                out.println("</tr>");

                                                            }
                                                        }

                                                    }

                                                }

                                            } else {

                                                if (q == 2) {
                                                    for (int i2 = 15; i2 < day; i2++) {

                                                        List<Timecard> timecards = bean.getDatesByDate(year + "-" + month + "-" + (i2 + 1), id_empl);

                                                        if (timecards != null && timecards.size() > 0) {

                                                            for (int i = 0; i < timecards.size(); i++) {
                                                                out.println("<tr>");

                                                                out.println("<td style=\"text-align: center;\"><a onclick=\"return popitup('timecard.jsp?id=" + timecards.get(i).getId() + "')\"href=\"timecard.jsp?id=" + timecards.get(i).getId() + "\">" + year + "-" + month + "-" + (i2 + 1) + "</a></td>");

                                                                // out.print(timecards.get(i).getId() + " - ");
                                                                if (timecards.get(i).getIn() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(i).getIn()) + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }
                                                                if (timecards.get(i).getOut() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(i).getOut()) + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }
                                                                if (timecards.get(i).getReason() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + timecards.get(i).getReason() + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }

                                                                out.println("</tr>");
                                                            }
                                                        }

                                                    }

                                                } else {
                                                    for (int i = 0; i < 15; i++) {  //Previous week
                                                        // System.out.println(year + "-" + month + "-" + (i + 1));
                                                        List<Timecard> timecards = bean.getDatesByDate(year + "-" + month + "-" + (i + 1), id_empl);

                                                        if (timecards != null && timecards.size() > 0) {

                                                            for (int k = 0; k < timecards.size(); k++) {
                                                                out.println("<tr>");

                                                                out.println("<td style=\"text-align: center;\"><a onclick=\"return popitup('timecard.jsp?id=" + timecards.get(k).getId() + "')\" href=\"timecard.jsp?id=" + timecards.get(k).getId() + "\">" + year + "-" + month + "-" + (i + 1) + "</a></td>");

                                                                // out.print(timecards.get(i).getId() + " - ");
                                                                if (timecards.get(k).getIn() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(k).getIn()) + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }
                                                                if (timecards.get(k).getOut() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(k).getOut()) + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }
                                                                if (timecards.get(k).getReason() != null) {
                                                                    out.print("<td style=\"text-align: center;\">" + timecards.get(k).getReason() + " </td> ");
                                                                } else {
                                                                    out.print("<td></td>");
                                                                }

                                                                out.println("</tr>");

                                                            }
                                                        }
                                                    }

                                                }

                                            }

                                        } else {
                                        }

                                    } catch (Exception e) {
                                    }
                                    // Ends try to show table of dates

                                %>
                                <tr>
                                    <td width="20"></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                            </table>

                            <br /><br />
                        </div>


                    </div>

                    <div id="fix"><br /> </div>   
                </div>

            </div>
        </div>
    </body>
</html>
<%        } else {
            response.sendRedirect("punchit.jsp?last=mgrdash"); // Employee without MGR Privileges
            return;
        }
    } catch (Exception e) {
        //response.sendRedirect("index.jsp?error=1&last=mgrdash"); // Anything goes bad, get credentials once again
        return;
    }
%>